package cn.appsys.controller.backend;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.service.developer.AppCategoryService;
import cn.appsys.service.developer.AppInfoService;
import cn.appsys.service.developer.AppVersionService;
import cn.appsys.service.developer.DataDictionaryService;
import cn.appsys.util.PageSupport;
import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager/backend")
public class AppManagerController {

    @Autowired
    private AppInfoService appInfoService;

    @Autowired
    private DataDictionaryService dataDictionaryService;

    @Autowired
    private AppCategoryService appCategoryService;

    @Autowired
    private AppVersionService appVersionService;

    @RequestMapping("/app/list")
    public String appList(@RequestParam Map<String,Object> params, Model model){
        List<DataDictionary> flatFormList = dataDictionaryService.getDataDictionaryList("APP_FLATFORM");
        List<AppCategory> categoryLevel1List = appCategoryService.getAppCategoryListByParentId(null);

        String pageIndex=(String) params.get("pageIndex");
        if (StringUtils.isNullOrEmpty(pageIndex)){
            params.put("currentPageNo",1);
        }else {
            params.put("currentPageNo",Integer.parseInt(pageIndex));
        }
        params.put("pageSize",5);
        params.put("softwareName",params.get("querySoftwareName"));
        params.put("status","1");
        params.put("flatformId",params.get("queryFlatformId"));
        params.put("categoryLevel1",params.get("queryCategoryLevel1"));
        params.put("categoryLevel2",params.get("queryCategoryLevel2"));
        params.put("categoryLevel3",params.get("queryCategoryLevel3"));

        PageSupport<AppInfo> pageSupport = appInfoService.getPageSupport(params);
        model.addAttribute("pages",pageSupport);
        model.addAttribute("categoryLevel1List",categoryLevel1List);
        model.addAttribute("flatFormList",flatFormList);
        model.addAttribute("querySoftwareName",params.get("querySoftwareName"));
        model.addAttribute("queryStatus",params.get("queryStatus"));
        model.addAttribute("queryFlatformId",params.get("queryFlatformId"));
        model.addAttribute("queryCategoryLevel1",params.get("queryCategoryLevel1"));
        model.addAttribute("queryCategoryLevel2",params.get("queryCategoryLevel2"));
        model.addAttribute("queryCategoryLevel3",params.get("queryCategoryLevel3"));

        if (!StringUtils.isNullOrEmpty((String) params.get("queryCategoryLevel2"))){
            Integer pid=Integer.parseInt(params.get("queryCategoryLevel1").toString());
            List<AppCategory> categoryLevel2List = appCategoryService.getAppCategoryListByParentId(pid);
            model.addAttribute("categoryLevel2List",categoryLevel2List);
        }
        if (!StringUtils.isNullOrEmpty((String) params.get("queryCategoryLevel3"))){
            Integer pid=Integer.parseInt(params.get("queryCategoryLevel2").toString());
            List<AppCategory> categoryLevel3List = appCategoryService.getAppCategoryListByParentId(pid);
            model.addAttribute("categoryLevel3List",categoryLevel3List);
        }

        return "backend/applist";
    }

    @RequestMapping(value = "/app/categorylevellist",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    @ResponseBody
    public Object categorylevellist(String pid){

        List<AppCategory> appCategoryListByParentId
                = appCategoryService.getAppCategoryListByParentId(pid.equals("")?null:Integer.parseInt(pid));

        return JSON.toJSONString(appCategoryListByParentId);
    }

    @RequestMapping("/app/check")
    public String check(Integer vid,Integer aid,Model model){
        AppVersion version = appVersionService.getAppVersionById(vid);
        AppInfo appInfo = appInfoService.getAppInfo(aid);
        model.addAttribute(appInfo);
        model.addAttribute(version);
        return "/backend/appcheck";
    }

    @RequestMapping("/app/checksave")
    public String checksave(Integer id,Integer status){
        if (appInfoService.updateStatus(status,id)){
            return "redirect:/manager/backend/app/list";
        }
        return "/backend/appcheck";
    }
}
